﻿using System;
using System.IO;
using UnityEngine;
using UnityEditor;

namespace ZXthex
{
    /// <summary>
    /// SystemConsole的运行设定
    /// </summary>
    public class UnityCommandLineSetting
    {

        /// <summary>
        /// 打开控制台窗口
        /// </summary>
        [MenuItem("控制台窗口/打开")]
        static void OpenConsole()
        {
            ConsoleObject.GetConsoleObject().stateShouldOpen = true;
            ConsoleHelper.OpenConsole();
        }

        /// <summary>
        /// 关闭控制台窗口
        /// </summary>
        [MenuItem("控制台窗口/关闭")]
        static void CloseConsole()
        {
            ConsoleObject.GetConsoleObject().stateShouldOpen = false;
            ConsoleHelper.CloseConsole();
        }

        [MenuItem("控制台窗口/设置")]
        static void GotoSetting()
        {
            var obj = (ConsoleObject.GetConsoleObject());
            Selection.activeObject = obj;
            EditorGUIUtility.PingObject(obj);
        }

        /// <summary>
        /// 点击Play会自动打开控制台
        /// </summary>
        public static void OpenConsoleOnEnter()
        {
            ConsoleObject.GetConsoleObject().startOnStartPlay = true;
        }

        /// <summary>
        /// 点击Play不会自动打开控制台
        /// </summary>
        public static void StopOpenConsoleOnEnter()
        {
            ConsoleObject.GetConsoleObject().startOnStartPlay = false;
        }

        /// <summary>
        /// 结束Play时，控制台不会自动关闭
        /// </summary>
        public static void MakeConsoleLiveOnPlayOver()
        {
            ConsoleObject.GetConsoleObject().liveOnEndPlay = true;
        }

        /// <summary>
        /// 结束Play时，控制台会自动关闭
        /// </summary>
        public static void MakeConsoleDeadOnPlayOver()
        {
            ConsoleObject.GetConsoleObject().liveOnEndPlay = false;
        }

        
        [InitializeOnLoadMethod]
        static void InitOnLoad()
        {
            //ctrl_c_callback属于非Unity主线程的回调，所以无法直接调用UnityEditor的即时指令
            ConsoleHelper.ctrl_c_callback += () =>
                EditorApplication.delayCall += () => ConsoleObject.GetConsoleObject().stateShouldOpen = false;

            if (ConsoleObject.GetConsoleObject().stateShouldOpen)
                EditorApplication.delayCall += () => ConsoleHelper.OpenConsole();
        }

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
        static void StartGame()
        {
            if (ConsoleObject.GetConsoleObject().startOnStartPlay
                || ConsoleObject.GetConsoleObject().stateShouldOpen)
                OpenConsole();

            switch (ConsoleObject.GetConsoleObject().cleanOutputOnStartPlay)
            {
                case ConsoleObject.CleanStyle.Clean:
                    Console.Clear();
                    break;
                case ConsoleObject.CleanStyle.MarkLine:
                    Console.WriteLine();
                    int count = Mathf.Min(Console.BufferWidth, Console.WindowWidth, 8);
                    Console.Write("".PadRight(count, '='));
                    Console.WriteLine();
                    break;
            }

            EditorApplication.playModeStateChanged += (x) =>
            {
                if (ConsoleObject.GetConsoleObject().cleanInputOnStartPlay)
                    ConsoleHelper.CleanConsoleInput();

                if (!ConsoleObject.GetConsoleObject().liveOnEndPlay)
                {
                    if (x == PlayModeStateChange.ExitingPlayMode)
                    {
                        CloseConsole(); 
                    }
                }
            };
        }


    }

}